package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.dao.ToolMapper;
import com.lucher.mes.dao.ToolplateMapper;
import com.lucher.mes.dao.ToolplateinfoMapper;
import com.lucher.mes.po.Tool;
import com.lucher.mes.po.Toolplate;
import com.lucher.mes.po.Toolplateinfo;

@Controller
@RequestMapping("/toolplate")  
public class ToolplateController extends BaseController{
	@Autowired
	private ToolplateMapper toolplateDao;
	@Autowired
	private ToolplateinfoMapper toolplateinfoDao;
	@Autowired
	private ToolMapper toolDao;
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Toolplate form) {
		return "toolplate/findToolplateList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Toolplate form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = toolplateDao.findByCount(form);
		List<Toolplate> list = toolplateDao.findByListForPage(form);
		
		for (Toolplate toolplate : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", toolplate.getId());
			objectMap.put("toolplatecode", toolplate.getToolplatecode());
			objectMap.put("toolplatename", toolplate.getToolplatename());
			objectMap.put("rownums", toolplate.getRownums());
			objectMap.put("columnnums", toolplate.getColumnnums());
			objectMap.put("racknum", toolplate.getRacknum());
			objectMap.put("toolcode", toolplate.getToolcode());
			objectMap.put("toolname", toolplate.getToolname());
			objectMap.put("tooltype", toolplate.getTooltype());
			objectMap.put("totalnum", toolplate.getTotalnum());
			objectMap.put("leftnum", toolplate.getLeftnum());
			objectMap.put("outnum", toolplate.getOutnum());
			objectMap.put("remark", toolplate.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 添加信息初始化
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addInit")
	public String addInit(Toolplate form,Model model) {
		model.addAttribute("toollist", toolDao.findByList(new Tool()));
		return "toolplate/addToolplate";
	}

	/**
	 * 新增
	 * @throws Exception 
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/save")
	@Transactional //启动事务
	public Map<String,Object>  save(Toolplate form){
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getToolplatecode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入货柜编码");
			return responseJson;
		} else {
			Toolplate oldToolplate = toolplateDao.findByToolplatecode(form.getToolplatecode());
			if (oldToolplate != null) {
				responseJson.put("flag", "0");//操作失败
				responseJson.put("msg", "操作失败，此货柜编码已经存在");
				return responseJson;
			}
		}
		
		if ("".equals(form.getToolcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请选择存放工具");
			return responseJson;
		}
		
		Tool tool = toolDao.findByToolcode(form.getToolcode());
		if (tool == null) {
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，该工具编号不存在");
			return responseJson;
		}
		
		Toolplate oldToolplate = toolplateDao.findByToolcode(form.getToolcode());
		if (oldToolplate != null) {
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，此工具编号已经对应存在工具板");
			return responseJson;
		}
		
		
		//保存货柜信息
		form.setToolname(tool.getToolname());
		form.setTooltype(tool.getTooltype());
		form.setRacknum(form.getRownums()*form.getColumnnums()  );
		
		//入库总量0
		form.setTotalnum(0);
		//库存剩存0
		form.setLeftnum(0);
		//借出量0
		form.setOutnum(0);
		Integer toolplateId = toolplateDao.insertSelective(form);
		
		//保存成功
		if(toolplateId != 0){
			ArrayList<Toolplateinfo> toolplateinfoList = new ArrayList<Toolplateinfo>();
			ArrayList<String> strList = form.getStrList();
			for (int i = 1; i < strList.size(); i++) {
				Toolplateinfo toolplateinfo = new Toolplateinfo();
				toolplateinfo.setToolplatecode(form.getToolplatecode());
				toolplateinfo.setToolplatename(form.getToolplatename());
				toolplateinfo.setRownum(Integer.parseInt(strList.get(i).split("-")[1]));
				toolplateinfo.setColumnnum(Integer.parseInt(strList.get(i).split("-")[2]));
				toolplateinfo.setCodeinfo(strList.get(i));
				toolplateinfo.setStatus("空位");
				toolplateinfoList.add(toolplateinfo);
				//toolplateinfoDao.insertSelective(toolplateinfo);
			}
			toolplateinfoDao.saveBatch(toolplateinfoList);
		}
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}

	/**
	 * 更新初始化
	 */
	@RequestMapping(value = "/updateInit")
	public String updateInit(Toolplate form,Toolplateinfo record,Model model) {
		
		model.addAttribute("toollist", toolDao.findByList(new Tool()));
		
		Toolplate toolplate = toolplateDao.selectByPrimaryKey(form.getId());
		form.setToolplate(toolplate);

		record.setToolplatecode(toolplate.getToolplatecode());
		List<Toolplateinfo> toolplateinfoList = toolplateinfoDao.findByList(record);
		form.setToolplateinfolist(toolplateinfoList);
		
		return "toolplate/updateToolplate";
	}

	/**
	 * 更新
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value = "/update")
	public Map<String,Object> update(Toolplate form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		if ("".equals(form.getToolplatecode())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，货柜编码不能为空");
			return responseJson;
		} else {
			Toolplate oldToolplate = toolplateDao.findByToolplatecode(form.getToolplatecode());
			if (oldToolplate != null && !oldToolplate.getId().equals(form.getId())) {
				responseJson.put("flag", "0"); //添加失败
				responseJson.put("msg", "操作失败，货柜编码已经存在");
				return responseJson;
			}
		}

		if ("".equals(form.getToolcode())) {
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，请输入存放工具");
			return responseJson;
		} 
		
		Tool tool = toolDao.findByToolcode(form.getToolcode());
		if (tool == null) {
			responseJson.put("flag", "0");//操作失败
			responseJson.put("msg", "操作失败，该工具编号不存在");
			return responseJson;
		}
		
		Toolplate oldToolplate = toolplateDao.findByToolcode(form.getToolcode());
		if (oldToolplate != null && !oldToolplate.getId().equals(form.getId())) {
			responseJson.put("flag", "0"); //添加失败
			responseJson.put("msg", "操作失败，此工具编号已经对应存在工具板");
			return responseJson;
		}
		
		//保存货柜信息
		form.setToolname(tool.getToolname());
		//重新计算工具面板仓位
		form.setRacknum(form.getRownums()*form.getColumnnums()  );
		tool.setTooltype(tool.getTooltype());

		
		// 修改信息
		int flag = toolplateDao.updateByPrimaryKeySelective(form);
		
		if(flag != 0){//修改成功
			//删除旧的仓位
			toolplateinfoDao.deleteByToolplatecode(form.getToolplatecode());
			//依次增加新的仓位
			ArrayList<Toolplateinfo> toolplateinfoList = new ArrayList<Toolplateinfo>();
			List<String> strList = form.getStrList();
			for (int i = 1; i < strList.size(); i++) {
				Toolplateinfo toolplateinfo = new Toolplateinfo();
				toolplateinfo.setToolplatecode(form.getToolplatecode());
				toolplateinfo.setToolplatename(form.getToolplatename());
				toolplateinfo.setRownum(Integer.parseInt(strList.get(i).split("-")[1]));
				toolplateinfo.setColumnnum(Integer.parseInt(strList.get(i).split("-")[2]));
				toolplateinfo.setCodeinfo(strList.get(i));
				toolplateinfo.setStatus("空位");
				toolplateinfoList.add(toolplateinfo);
				//toolplateinfoDao.insertSelective(toolplateinfo);
			}
			toolplateinfoDao.saveBatch(toolplateinfoList);
		}
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		return responseJson;
	}
    
	/**
	 * 删除
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/delete")
	public Map<String,Object> delete(Toolplate form) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		// 删除网络，实际上就是修改state为0-无效
		Toolplate toolplatedmp = toolplateDao.selectByPrimaryKey(form.getId());
		
		//删除货柜
		toolplateDao.deleteByPrimaryKey(form.getId());
				
		//删除旧的仓位
		toolplateinfoDao.deleteByToolplatecode(toolplatedmp.getToolplatecode());
		
		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 删除操作员
	 * @return
	 */
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	@RequestMapping(value="/deleteBatch")
	public Map<String,Object> deleteBatch(@RequestBody List<Toolplate> formlist) {
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		for (Toolplate toolplate : formlist) {
			//删除货柜
			toolplateDao.deleteByPrimaryKey(toolplate.getId());
			//删除旧的仓位
			toolplateinfoDao.deleteByToolplatecode(toolplate.getToolplatecode());
		}

		responseJson.put("flag", "1");//删除成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
}
